<template>
	<cu-custom bgColor="" :isBack="true" show-circle>
		<template #title>日程表</template>
	</cu-custom>
	
	<view class="page-content">
		<view class="mb-3 theme-shadow">
			<Calendar :schedule="scheduleData" @change="dateChange" />
		</view>
		
		<view class="mb-3 flex justify-between">
			
			<view class="middle-text text-bold text-white">当日任务</view>
			<view class="middle-text text-white" @click="handleCreate">
				<text class="cuIcon-calendar mr-1"></text>
				<text>添加日程</text>
			</view>
		</view>
		
		<view v-for="(item,index) in currentSchedule" :key="index" class="schedule-label-box" @click="nav(item)" @longpress="handleDelete(item)">
			<ScheduleLabel :title="item.title" :type="item.type" :content="item.content" />
		</view>
		
		<view style="height: 88rpx;"></view>
	</view>
	
	<Dialog
		:message="dialogMessage"
		:type="dialogType"
		:cancelable="dialogCancelable"
		v-model:show="dialogShow"
		@confirm="handleConfirm"
	/>
	
	
</template>

<script lang="ts" setup>
import { getSchedule, deleteSchedule } from "@/api/schedule.js"
import { onShow } from "@dcloudio/uni-app"
import { onMounted, ref, type Ref } from "vue"
import Calendar from "./components/Calendar.vue"
import ScheduleLabel from "./components/ScheduleLabel.vue"
import { router } from "@/router"
import Dialog from "@/components/Dialog.vue"
import store from "@/store/index.js"
interface IMark {
	id: number
	time: string
	scheduleType: string
	type: string
	title: string
	content: string
	relatedId?: number
}

const dialogShow:Ref<boolean> = ref(false)
const dialogMessage:Ref<string> = ref("确认删除此日程")
const dialogType:Ref<string> = ref("info")
const dialogCancelable:Ref<boolean> = ref(true)
const scheduleData:Ref<IMark[]> = ref([] as IMark[])
const mockData:Ref<IMark[]> = ref([] as IMark[])
const currentSchedule:Ref<IMark[]> = ref([] as IMark[])

const dateChange = (e:any):void => {
	currentSchedule.value = e.schedule
}
const handleCreate = ():void => {
	router.push("/pages/schedule/create")
}

const loadData = ():void => {
	scheduleData.value = []
	getSchedule()
		.then((res) => {
			const data = res.data
			const scheduleList = []
			data.map((item) => {
				if (item.endTime && item.creatTime && (item.endTime !== item.creatTime)) {
					if (item.type == "1") {
						scheduleList.push({
							id: item.id,
							time: item.creatTime,
							type: "daily",
							scheduleType: item.type,
							title: "[开始] " + item.title,
							content: item.schoolName,
							relatedId: item.relatedId
						})
						scheduleList.push({
							id: item.id,
							time: item.endTime,
							type: "special",
							scheduleType: item.type,
							title: "[结束] " + item.title,
							content: item.schoolName,
							relatedId: item.relatedId
						})
					} else {
						let currTime = item.startTime
						while (currTime !== item.endTime) {
							scheduleList.push({
								id: item.id,
								time: currTime,
								type: "daily",
								scheduleType: item.type,
								title: item.title,
								content: item.schoolName,
								relatedId: item.relatedId
							})
							currTime = (new Date((new Date(currTime)).getTime() + 86400000)).format("yyyy-MM-dd")
						}
						scheduleList.push({
							id: item.id,
							time: currTime,
							type: "daily",
							scheduleType: item.type,
							title: item.title,
							content: item.schoolName,
							relatedId: item.relatedId
						})
					}
				} else {
					scheduleList.push({
						id: item.id,
						time: item.endTime || item.creatTime,
						type: "daily",
						scheduleType: item.type,
						title: item.title,
						content: item.schoolName,
						relatedId: item.relatedId
					})
				}
				
			})
			scheduleData.value = scheduleList
			processSchedule()
		})
		.catch((err) => {
			console.log(err)
		})
}

const processSchedule = () => {
	currentSchedule.value = []
	const date = new Date().format("yyyy-MM-dd")
	for (let i in scheduleData.value) {
		if (scheduleData.value[i].time === date) {
			currentSchedule.value.push(scheduleData.value[i])
		}
	}
}
const nav = (item:IMark):void => {
	if (item.scheduleType == "1") {
		router.push("/pages/mission/mission?mission_id=" + item.relatedId)
	}
}

let currItem:IMark = null
const handleDelete = (item:IMark) => {
	// if (store.getters.userInfo.role === 1) {
	if (item.scheduleType == "2") {
		dialogShow.value = true
		currItem = item
	}
	
	// }
}

const handleConfirm = () => {
	deleteSchedule(currItem.id)
		.then(() => {
			uni.showToast({
				title: "操作成功"
			})
			dialogShow.value = false
			loadData()
		})
}

onShow(() => {
	loadData()
})

onMounted(() => {
	// console.log(mockData.value)
	// mockData.value = [
	// 	{ time: "2023-10-31", type: "daily", title: "实地考察工作", "content": "8:00-16:00"},
	// 	{ time: "2023-10-14", type: "special", title: "专项督导任务截至日期", "content": "全天"},
	// 	{ time: "2023-10-14", type: "daily", title: "实地考察工作", "content": "8:00-16:00"}
	// ]
	
})


</script>

<style lang="stylus">
.schedule-label-box:nth-child(odd)
	background-color #5789e3
	
.page-content
	min-height: 100vh
	background-color: #3974df
</style>
